<template>
  <div class="contain">
    <div class="from">
      <h3>用户注册</h3>
      <el-form
        ref="ruleForm"
        :model="user"
        status-icon
        :rules="rules"
        label-width="130px"
      >
        <el-form-item label="邮箱" prop="mail">
          <el-input
            v-model="user.mail"
            type="text"
            placeholder="请输入邮箱"
            style="width: 200px; margin-right: 5px"
          />
          <span v-if="sending"
            ><el-button type="success" disabled>{{
              leftSecond
            }}</el-button></span
          >
          <span v-else
            ><el-button type="success" @click="send"
              >发送验证码</el-button
            ></span
          >
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            v-model="user.password"
            type="password"
            placeholder="请输入密码"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="邮箱验证码" prop="code">
          <el-input v-model="user.code" type="text" style="width: 200px" />
        </el-form-item>
      </el-form>
      <div class="submit">
        <el-button type="primary" @click="register" style="width: 350px"
          >注册</el-button
        >
      </div>
      <br />
      <router-link to="/login">已有账号？点击登录</router-link>
    </div>
  </div>
</template>

<script>
import RegApi from '@/api/core/LoginAndReg.js'
export default {
  name: 'Register',
  data() {
    var checkEmail = (rule, value, callback) => {
      const reg =
        /^[A-Za-z0-9]+([_.][A-Za-z0-9]+)*@([A-Za-z0-9-]+\.)+[A-Za-z]{2,6}$/
      if (!reg.test(value)) {
        callback(new Error('请输入有效的邮箱'))
      }
      callback()
    }
    return {
      user: {},
      sending: false,
      second: 10,
      leftSecond: 10,
      rules: {
        mail: [
          {
            required: true,
            message: '请输入邮箱',
            trigger: 'blur',
          },
          {
            validator: checkEmail,
            trigger: 'blur',
          },
        ],
        password: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur',
          },
          {
            min: 6,
            max: 15,
            message: '密码长度要在6~15之间',
            trigger: 'blur',
          },
        ],
        code: [
          {
            required: true,
            message: '请输入验证码',
            trigger: 'blur',
          },
        ],
      },
    }
  },

  methods: {
    //发送邮箱验证码
    send() {
      if (!this.user.mail) {
        this.$message.error('请输入邮箱')
        return
      }

      if (this.sending) return
      this.sending = true

      this.timeDown()

      RegApi.send(this.user.mail)
        .then((response) => {
          this.$message.success(response.message)
        })
        .catch((error) => {
          this.$message.error(error.message)
        })
    },

    //验证码倒计时
    timeDown() {
      this.leftSecond = this.second
      const timmer = setInterval(() => {
        this.leftSecond--
        if (this.leftSecond <= 0) {
          clearInterval(timmer)
          this.leftSecond = this.second
          this.sending = false
        }
      }, 1000)
    },

    //用户注册
    register() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          const code = this.user.code
          RegApi.regiest(this.user, code)
            .then(() => {
              this.$router.push({ path: '/login' })
            })
            .catch((error) => {
              this.$message.error(error.message)
            })
        } else {
          this.$message.error('表单没有按规则填写')
        }
      })
    },
  },
}
</script>

<style scoped>
.contain {
  width: 100%;
  height: 100%;
  background-image: url('/src/assets/image/login.jpg');
  position: fixed;
  background-size: 100% 100%;
}
.from {
  padding-top: 30px;
  width: 500px;
  height: 300px;
  margin-top: 340px;
  margin-left: 200px;
  background-color: aliceblue;
  text-align: center;
}
</style>
